package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.contatns.MQConstants;
import com.heima.common.contatns.RedisConstants;
import com.heima.common.dtos.AppHttpCodeEnum;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.exception.LeadNewsException;
import com.heima.model.article.dtos.UpdateArticleMsg;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.common.JsonUtils;
import com.heima.utils.common.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @Override
    public ResponseResult likesBehavior(LikesBehaviorDto dto) {

        //校验参数
        if(dto.getArticleId()==null || dto.getOperation()==null){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //判断是否登录
        ApUser apUser = (ApUser)ThreadLocalUtils.get();
        if(apUser==null){
            throw new LeadNewsException(AppHttpCodeEnum.NEED_LOGIN);
        }

        //redisTemplate.opsForValue().set("key","value");
        //redisTemplate.boundValueOps("key").set("value");

        String key = RedisConstants.LIKE_BEHAVIOR+dto.getArticleId();

        //BoundHashOperations: 该对象是一个Map集合，代表绑定了redis的指定key的value值对象
        BoundHashOperations<String, Object, Object> boundHashOps = redisTemplate.boundHashOps(key);

        //判断点赞还是取消点赞
        if(dto.getOperation().equals((short)0)){
            //判断是否已经点赞过
            if(boundHashOps.hasKey(apUser.getId().toString())){
                throw new LeadNewsException(500,"你已经点赞过，无需重复点赞");
            }

            //点赞，在redis添加记录
            boundHashOps.put(apUser.getId().toString(), JsonUtils.toString(dto));

            //发送消息给KafkaStream统计
            UpdateArticleMsg updateArticleMsg = new UpdateArticleMsg();
            updateArticleMsg.setArticleId(dto.getArticleId());
            updateArticleMsg.setType(UpdateArticleMsg.UpdateArticleType.LIKES);

            kafkaTemplate.send(MQConstants.HOT_ARTICLE_INPUT_TOPIC,JsonUtils.toString(updateArticleMsg));
        }else{
            //取消点赞从redis删除记录
            boundHashOps.delete(apUser.getId().toString());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);


        //判断点赞还是取消点赞
        /*if(dto.getOperation().equals((short)0)){
            //点赞，在redis添加记录
            redisTemplate.opsForHash().put(key,apUser.getId().toString(),JsonUtils.toString(dto));
        }else{
            //取消点赞从redis删除记录
            redisTemplate.opsForHash().delete(key,apUser.getId().toString());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);*/

    }
}
